package com.example.warehouse.bus.service.impl;

import com.example.warehouse.bus.dao.persist.repository.IBusCustomerRepository;
import com.example.warehouse.bus.dao.persist.repository.IBusProviderRepository;
import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.entity.BusProvider;
import com.example.warehouse.bus.pojo.param.BusCustomerAddNewParam;
import com.example.warehouse.bus.pojo.param.BusProviderAddNewParam;
import com.example.warehouse.bus.pojo.vo.BusCustomerStandardVO;
import com.example.warehouse.bus.pojo.vo.BusProviderStandardVO;
import com.example.warehouse.bus.service.IBusProviderService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Linger
 * @date 2023-07-07 8:59
 * @desciption:处理供应商数据的业务实体类
 */
@Slf4j
@Service
public class BusProviderServiceImpl implements IBusProviderService {
    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IBusProviderRepository busProviderRepository;

    public BusProviderServiceImpl() {
        log.info("创建控制器对象:BusProviderServiceImpl");
    }

    @Override
    public void addNewBusProvider(BusProviderAddNewParam busProviderAddNewParam) {
        log.debug("开始处理【添加供应商】的业务，参数：{}",busProviderAddNewParam);
        // 检查供应商名是否被占用
        {
            String provideName = busProviderAddNewParam.getProviderName();
            int count = busProviderRepository.countByProviderName(provideName);
            if (count > 0) {
                String message = "添加供应商失败，供应商名已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }
        // 检查供应商手机号码是否被占用
        {
            String phone = busProviderAddNewParam.getPhone();
            int count = busProviderRepository.countByProviderPhone(phone);
            if (count > 0) {
                String message = "添加供应商失败，手机号码已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }

        // 检查供应商电子邮箱是否被占用
        {
            String email = busProviderAddNewParam.getEmail();
            int count =busProviderRepository.countByProviderEmail(email);
            if (count > 0) {
                String message = "添加供应商失败，电子邮箱已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }

        BusProvider busProvider = new BusProvider();
        BeanUtils.copyProperties(busProviderAddNewParam, busProvider);
        int rows = busProviderRepository.insert(busProvider);
        if (rows != 1) {
            String message = "添加客户失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除供应商】的业务，参数：{}", id);
        Object queryResult = busProviderRepository.getStandardByProviderId(id);
        if (queryResult == null) {
            String message = "删除供应商失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        log.debug("即将执行删除数据，参数：{}", id);
        int rows =busProviderRepository.deleteByProviderId(id);
        if (rows != 1) {
            String message = "删除供应商失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }

    }

    @Override
    public void update(BusProvider busProvider) {
        log.debug("开始处理【修改供应商】的业务，参数：{}", busProvider);
        Object queryResult =busProviderRepository.getStandardByProviderId(busProvider.getId());
        if (queryResult == null) {
            String message = "修改供应商失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = busProviderRepository.update(busProvider);
        if (rows != 1) {
            String message = "修改供应商失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public BusProviderStandardVO getStandardByProviderId(Long id) {
        log.debug("开始处理【根据id查询供应商】的业务，参数：{}", id);
        return busProviderRepository.getStandardByProviderId(id);
    }

    @Override
    public PageData<BusProviderStandardVO> queryByKeyWord(Integer pageNum, String providerName, String connectionPerson, String phone) {
        log.debug("开始处理【模糊查询】的业务，参数：{},{},{}",providerName, connectionPerson, phone);
        return busProviderRepository.queryByKeyWord(pageNum, defaultQueryPageSize,providerName, connectionPerson, phone);
    }

    @Override
    public PageData<BusProvider> list(Integer pageNum) {
        log.debug("开始处理【查询供应商列表】的业务，页码：{}", pageNum);
        PageData<BusProvider> pageData = busProviderRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusProvider> listAsc(Integer pageNum) {
        log.debug("开始处理【升序查询供应商列表】的业务，页码：{}", pageNum);
        PageData<BusProvider> pageData = busProviderRepository.listAsc(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusProvider> listDesc(Integer pageNum) {
        log.debug("开始处理【降序查询供应商列表】的业务，页码：{}", pageNum);
        PageData<BusProvider> pageData = busProviderRepository.listDesc(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusProvider> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询供应商列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<BusProvider> pageData = busProviderRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public void setProviderDisable(Long id) {
        updateEnableByProviderId(id, 0);
    }

    @Override
    public void setProviderEnable(Long id) {
        updateEnableByProviderId(id, 1);
    }

    private void updateEnableByProviderId(Long id, Integer enable) {
        log.debug("开始处理【{}供应商】的业务，ID：{}，目标状态：{}", ENABLE_TEXT[enable], id, enable);
        BusProviderStandardVO queryResult = busProviderRepository.getStandardByProviderId(id);
        if (queryResult == null) {
            String message = ENABLE_TEXT[enable] + "供应商失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (queryResult.getEnable().equals(enable)) {
            String message = ENABLE_TEXT[enable] + "供应商失败，当前用户已经处于"
                    + ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        BusProvider user = new BusProvider();
        user.setId(id);
        user.setEnable(enable);
        log.debug("即将修改数据，参数：{}", user);
        int rows = busProviderRepository.updateByProviderId(user);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "供应商失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}
